package com.example.doctor.service;
import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.alibaba.fastjson.JSONObject;
import com.example.common.Dto;
import com.example.common.DtoUtil;
import com.example.pojo.GdDoctor;
import com.example.pojo.GdInfoDic;
import com.example.pojo.GdUser;
import com.example.pojo.GdUserDoctorAttention;
import com.example.service.RpcDoctorService;
import com.example.service.RpcLoginService;
import com.example.utils.EmptyUtils;
import com.example.vo.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by LENOVO on 2018/9/23.
 */
@Component
public class DoctorServiceImpl implements DoctorService{

    @DubboConsumer
    private RpcDoctorService rpcDoctorService;

    @DubboConsumer
    private RpcLoginService rpcLoginService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public DoctorDetailsReturnVo getDotorDetailsById(String id) throws Exception {

        if(id==null)
        {
            throw new Exception("医生Id为空");
        }
        return rpcDoctorService.selectDetailsById(Long.parseLong(id));
    }

    @Override
    public DoctorIntroduceReturnVo getDotorIntroduceById(String id) throws Exception {

        if(id==null)
        {
            throw new Exception("医生Id为空");
        }
        return rpcDoctorService.selectDotorIntroduceById(Long.parseLong(id));
    }

    @Override
    public boolean attentionDoctor(String doctorId,HttpServletRequest request) throws Exception {

        String token = request.getHeader("token");
        String info=stringRedisTemplate.opsForValue().get(token);
        GdUser user = JSONObject.parseObject(info,GdUser.class);
        GdUserDoctorAttention gdUserDoctorAttention=new GdUserDoctorAttention();
        gdUserDoctorAttention.setUserid(user.getGd());
        gdUserDoctorAttention.setDoctorid(Long.parseLong(doctorId));
        gdUserDoctorAttention.setCreationdate(new Date());
        return rpcDoctorService.attentionDoctor(gdUserDoctorAttention);
    }

    @Override
    public boolean cancelAttentionDoctor(String doctorId,HttpServletRequest request) throws Exception {

        String token = request.getHeader("token");
        String info=stringRedisTemplate.opsForValue().get(token);
        GdUser user = JSONObject.parseObject(info,GdUser.class);
        GdUserDoctorAttention gdUserDoctorAttention=new GdUserDoctorAttention();
        gdUserDoctorAttention.setUserid(user.getGd());
        gdUserDoctorAttention.setDoctorid(Long.parseLong(doctorId));
        return rpcDoctorService.cancelAttentionDoctor(gdUserDoctorAttention);
    }

    /***
     * 发送订单消息
     * @param orderVo
     * @return
     */
    @Override
    public Dto sendOrderMessage(OrderVo orderVo, HttpServletRequest request) throws Exception{
        String token = request.getHeader("token");
        String info=stringRedisTemplate.opsForValue().get(token);
        GdUser user = JSONObject.parseObject(info,GdUser.class);
        if(EmptyUtils.isEmpty(user)){
            return DtoUtil.returnFail("用户未登录","0001");
        }
        OrderMessageVo orderMessageVo=new OrderMessageVo();
        orderMessageVo.setUserId(user.getGd().toString());
        orderMessageVo.setServiceId(orderVo.getServiceid());
        orderMessageVo.setDoctorId(orderVo.getDoctorid());
        orderMessageVo.setAmount(orderVo.getPayamount().doubleValue());
        orderMessageVo.setPaytype(orderVo.getPaytype());
        return rpcDoctorService.sendOrderMessage(orderMessageVo);
    }

    @Override
    public List<DeptInfoVo> getDeptInfo() throws Exception {
        Map map=new HashMap<String,Object>();
        List<DeptInfoVo> deptInfoVoList=  new ArrayList<DeptInfoVo>();
        map.put("parentId",1);
        List<GdInfoDic> gdInfoDic=rpcDoctorService.getDeptInfo(map);
        DeptInfoVo deptInfoVo=new DeptInfoVo();
        for(GdInfoDic gdInfoDic1:gdInfoDic)
        {
            deptInfoVo.setId(gdInfoDic1.getId());
            deptInfoVo.setName(gdInfoDic1.getName());
            deptInfoVo.setDescription(gdInfoDic1.getDescription());
            deptInfoVo.setImageurl(gdInfoDic1.getImageurl());
            deptInfoVoList.add(deptInfoVo);
        }
        return deptInfoVoList;
    }

    @Override
    public List<GdDoctor> getRecommendDoctorList() throws Exception {
        Map map=new HashMap<String,Object>();
        map.put("start",0);
        map.put("end",3);
        return rpcDoctorService.getRecommendDoctorList(map);
    }
}
